/*
 * @Author: shijianxin
 * @Date: 2022-02-10 10:45:01
 * @Description: file content
 */

/**
 * 根据最小值和最大值获取随机数
 * @param min
 */
export function getRandom(min, max) {
  const dec = max - min + 1;
  return Math.floor(Math.random() * dec + min);
}

/**
 * 判断是否为数字
 */
export const isNumber = (num) => {
  return typeof num === "number" && !isNaN(num) && num !== Infinity;
};

/**
 * 递归获取id对应值
 */
export const getValueById = (id, arr) => {
  //判断list是否是数组
  if (!arr instanceof Array) {
    return null;
  }
  //遍历数组
  for (let i in arr) {
    let item = arr[i];
    if (item.id === id) {
      return item;
    } else {
      //查不到继续遍历
      if (item.childList) {
        let value = getValueById(id, item.childList);
        //查询到直接返回
        if (value) {
          return value;
        }
      }
    }
  }
};

/**
 * 页面滚动到顶部
 */
export const toTop = () => {
  let top = document.documentElement.scrollTop || document.body.scrollTop;
  // 实现滚动效果
  const timeTop = setInterval(() => {
    document.body.scrollTop = document.documentElement.scrollTop = top -= 50;
    if (top <= 0) {
      clearInterval(timeTop);
    }
  }, 10);
};

/**
 * 计算日期至今多少天
 */
export const getDateToNowDay = (date) => {
  const diff = (new Date().getTime() - new Date(date).getTime()) / 1000;
  return Math.floor(diff / 86400);
};

/**
 * 设置日期多久前
 * ios的new Date()不支持yyyy-MM-dd HH:mm:ss格式的日期 需要把格式改为yyyy/MM/dd HH:mm:ss
 */
export const getDateDiff = (date) => {
  if (!date) {
    return "-";
  }
  date = date.replace(/\-/g, "/");
  let minute = 1000 * 60;
  let hour = minute * 60;
  let day = hour * 24;
  let month = day * 30;
  let year = month * 12;
  let now = new Date().getTime();
  let oldTime = new Date(date).getTime();
  let diffValue = now - oldTime;
  if (diffValue < 0) {
    return;
  }
  let monthC = diffValue / month;
  let weekC = diffValue / (7 * day);
  let dayC = diffValue / day;
  let hourC = diffValue / hour;
  let minC = diffValue / minute;
  let yearC = diffValue / year;

  if (yearC >= 1) {
    return `${parseInt(yearC)}年前`;
  } else if (monthC >= 1) {
    return `${parseInt(monthC)}月前`;
  } else if (weekC >= 1) {
    return `${parseInt(weekC)}周前`;
  } else if (dayC >= 1) {
    return `${parseInt(dayC)}天前`;
  } else if (hourC >= 1) {
    return `${parseInt(hourC)}小时前`;
  } else if (minC >= 1) {
    return `${parseInt(minC)}分钟前`;
  } else {
    return "刚刚";
  }
};

/**
 * 获取浏览器版本
 */
export const getBrowseVersion = () => {
  let browser = {};
  let userAgent = navigator.userAgent.toLowerCase();
  let s;
  (s = userAgent.match(/msie ([\d.]+)/))
    ? (browser.ie = s[1])
    : (s = userAgent.match(/firefox\/([\d.]+)/))
    ? (browser.firefox = s[1])
    : (s = userAgent.match(/chrome\/([\d.]+)/))
    ? (browser.chrome = s[1])
    : (s = userAgent.match(/opera.([\d.]+)/))
    ? (browser.opera = s[1])
    : (s = userAgent.match(/version\/([\d.]+).*safari/))
    ? (browser.safari = s[1])
    : 0;
  let version = "";
  if (browser.ie) {
    version = "IE " + browser.ie;
  } else {
    if (browser.firefox) {
      version = "Firefox " + browser.firefox;
    } else {
      if (browser.chrome) {
        version = "Chrome " + browser.chrome;
      } else {
        if (browser.opera) {
          version = "Opera " + browser.opera;
        } else {
          if (browser.safari) {
            version = "Safari " + browser.safari;
          } else {
            version = "未知浏览器";
          }
        }
      }
    }
  }
  return version;
};

/**
 * 获取操作系统
 */
export const getOS = () => {
  let sUserAgent = navigator.userAgent;
  let isWin = navigator.platform == "Win32" || navigator.platform == "Windows";
  let isMac =
    navigator.platform == "Mac68K" ||
    navigator.platform == "MacPPC" ||
    navigator.platform == "Macintosh" ||
    navigator.platform == "MacIntel";
  if (isMac) return "Mac";
  let isUnix = navigator.platform == "X11" && !isWin && !isMac;
  if (isUnix) return "Unix";
  let isLinux = String(navigator.platform).indexOf("Linux") > -1;
  if (isLinux) return "Linux";
  if (isWin) {
    let isWin2K =
      sUserAgent.indexOf("Windows NT 5.0") > -1 ||
      sUserAgent.indexOf("Windows 2000") > -1;
    if (isWin2K) return "Windows2000";
    let isWinXP =
      sUserAgent.indexOf("Windows NT 5.1") > -1 ||
      sUserAgent.indexOf("Windows XP") > -1;
    if (isWinXP) return "WindowsXP";
    let isWin2003 =
      sUserAgent.indexOf("Windows NT 5.2") > -1 ||
      sUserAgent.indexOf("Windows 2003") > -1;
    if (isWin2003) return "Windows2003";
    let isWinVista =
      sUserAgent.indexOf("Windows NT 6.0") > -1 ||
      sUserAgent.indexOf("Windows Vista") > -1;
    if (isWinVista) return "WindowsVista";
    let isWin7 =
      sUserAgent.indexOf("Windows NT 6.1") > -1 ||
      sUserAgent.indexOf("Windows 7") > -1;
    if (isWin7) return "Windows7";
    let isWin10 =
      sUserAgent.indexOf("Windows NT 10") > -1 ||
      sUserAgent.indexOf("Windows 10") > -1;
    if (isWin10) return "Windows10";
  }
  return "other";
};
